﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Data;

namespace VSTODemo {
    public class JsonUtils {
        public static DataTable ToTable(string json) {
            var srcArray = JArray.Parse(json); // json格式约定最上层为 array

            var trgArray = new JArray();
            foreach (JObject row in srcArray.Children<JObject>()) {
                var cleanRow = new JObject();
                foreach (JProperty column in row.Properties()) {
                    // Only include JValue types
                    if (column.Value is JValue) {
                        cleanRow.Add(column.Name, column.Value);
                    }
                }

                trgArray.Add(cleanRow);
            }

            return JsonConvert.DeserializeObject<DataTable>(trgArray.ToString());
        }
    }
}
